home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / baraduke.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  10KB  |  406 lines

  1. #include "driver.h"
  2. #include "vidhrdw/generic.h"
  3. #include "tilemap.h"
  4.  
  5. unsigned char *baraduke_textram, *baraduke_videoram;
  6.  
  7. static struct tilemap *tilemap[2];    /* backgrounds */
  8. static int xscroll[2], yscroll[2];    /* scroll registers */
  9. static int flipscreen;
  10.  
  11. /***************************************************************************
  12.  
  13.     Convert the color PROMs into a more useable format.
  14.  
  15.     The palette PROMs are connected to the RGB output this way:
  16.  
  17.     bit 3    -- 220 ohm resistor  -- RED/GREEN/BLUE
  18.             -- 470 ohm resistor  -- RED/GREEN/BLUE
  19.             -- 1  kohm resistor  -- RED/GREEN/BLUE
  20.     bit 0    -- 2.2kohm resistor  -- RED/GREEN/BLUE
  21.  
  22. ***************************************************************************/
  23.  
  24. void baraduke_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  25. {
  26.     int i;
  27.     int bit0,bit1,bit2,bit3;
  28.  
  29.     for (i = 0; i < 2048; i++)
  30.     {
  31.         /* red component */
  32.         bit0 = (color_prom[2048] >> 0) & 0x01;
  33.         bit1 = (color_prom[2048] >> 1) & 0x01;
  34.         bit2 = (color_prom[2048] >> 2) & 0x01;
  35.         bit3 = (color_prom[2048] >> 3) & 0x01;
  36.         *(palette++) = 0x0e*bit0 + 0x1f*bit1 + 0x43*bit2 + 0x8f*bit3;
  37.  
  38.         /* green component */
  39.         bit0 = (color_prom[0] >> 0) & 0x01;
  40.         bit1 = (color_prom[0] >> 1) & 0x01;
  41.         bit2 = (color_prom[0] >> 2) & 0x01;
  42.         bit3 = (color_prom[0] >> 3) & 0x01;
  43.         *(palette++) = 0x0e*bit0 + 0x1f*bit1 + 0x43*bit2 + 0x8f*bit3;
  44.  
  45.         /* blue component */
  46.         bit0 = (color_prom[0] >> 4) & 0x01;
  47.         bit1 = (color_prom[0] >> 5) & 0x01;
  48.         bit2 = (color_prom[0] >> 6) & 0x01;
  49.         bit3 = (color_prom[0] >> 7) & 0x01;
  50.         *(palette++) = 0x0e*bit0 + 0x1f*bit1 + 0x43*bit2 + 0x8f*bit3;
  51.  
  52.         color_prom++;
  53.     }
  54. }
  55.  
  56. /***************************************************************************
  57.  
  58.     Callbacks for the TileMap code
  59.  
  60. ***************************************************************************/
  61.  
  62. static void get_tile_info0(int tile_index)
  63. {
  64.     unsigned char attr = baraduke_videoram[2*tile_index + 1];
  65.     unsigned char code = baraduke_videoram[2*tile_index];
  66.  
  67.     SET_TILE_INFO(1 + ((attr & 0x02) >> 1), code | ((attr & 0x01) << 8), attr);
  68. }
  69.  
  70. static void get_tile_info1(int tile_index)
  71. {
  72.     unsigned char attr = baraduke_videoram[0x1000 + 2*tile_index + 1];
  73.     unsigned char code = baraduke_videoram[0x1000 + 2*tile_index];
  74.  
  75.     SET_TILE_INFO(3 + ((attr & 0x02) >> 1), code | ((attr & 0x01) << 8), attr);
  76. }
  77.  
  78. /***************************************************************************
  79.  
  80.     Start the video hardware emulation.
  81.  
  82. ***************************************************************************/
  83.  
  84. int baraduke_vh_start( void )
  85. {
  86.     tilemap[0] = tilemap_create(get_tile_info0,tilemap_scan_rows,TILEMAP_TRANSPARENT,8,8,64,32);
  87.     tilemap[1] = tilemap_create(get_tile_info1,tilemap_scan_rows,TILEMAP_TRANSPARENT,8,8,64,32);
  88.  
  89.     if (!tilemap[0] || !tilemap[1])
  90.         return 1;
  91.  
  92.     tilemap[0]->transparent_pen = 7;
  93.     tilemap[1]->transparent_pen = 7;
  94.  
  95.     return 0;
  96. }
  97.  
  98. /***************************************************************************
  99.  
  100.     Memory handlers
  101.  
  102. ***************************************************************************/
  103.  
  104. READ_HANDLER( baraduke_videoram_r )
  105. {
  106.     return baraduke_videoram[offset];
  107. }
  108.  
  109. WRITE_HANDLER( baraduke_videoram_w )
  110. {
  111.     if (baraduke_videoram[offset] != data)
  112.     {
  113.         baraduke_videoram[offset] = data;
  114.         tilemap_mark_tile_dirty(tilemap[offset/0x1000],(offset&0xfff)/2);
  115.     }
  116. }
  117.  
  118. static void scroll_w(int layer,int offset,int data)
  119. {
  120.     int xdisp[2] = { 26, 24 };
  121.     int scrollx, scrolly;
  122.  
  123.     switch (offset)
  124.     {
  125.         case 0:    /* high scroll x */
  126.             xscroll[layer] = (xscroll[layer] & 0xff) | (data << 8);
  127.             break;
  128.         case 1:    /* low scroll x */
  129.             xscroll[layer] = (xscroll[layer] & 0xff00) | data;
  130.             break;
  131.         case 2:    /* scroll y */
  132.             yscroll[layer] = data;
  133.             break;
  134.     }
  135.  
  136.     scrollx = xscroll[layer] + xdisp[layer];
  137.     scrolly = yscroll[layer] + 25;
  138.     if (flipscreen)
  139.     {
  140.         scrollx = -scrollx + 227;
  141.         scrolly = -scrolly + 32;
  142.     }
  143.  
  144.     tilemap_set_scrollx(tilemap[layer], 0, scrollx);
  145.     tilemap_set_scrolly(tilemap[layer], 0, scrolly);
  146. }
  147.  
  148. WRITE_HANDLER( baraduke_scroll0_w )
  149. {
  150.     scroll_w(0, offset, data);
  151. }
  152. WRITE_HANDLER( baraduke_scroll1_w )
  153. {
  154.     scroll_w(1, offset, data);
  155. }
  156.  
  157. /***************************************************************************
  158.  
  159.     Display Refresh
  160.  
  161. ***************************************************************************/
  162.  
  163. static void draw_sprites(struct osd_bitmap *bitmap, int priority)
  164. {
  165.     const struct rectangle *clip = &Machine->drv->visible_area;
  166.  
  167.     const unsigned char *source = &spriteram[0];
  168.     const unsigned char *finish = &spriteram[0x0800-16];/* the last is NOT a sprite */
  169.  
  170.     int sprite_xoffs = spriteram[0x07f5] - 256 * (spriteram[0x07f4] & 1) + 16;
  171.     int sprite_yoffs = spriteram[0x07f7] - 256 * (spriteram[0x07f6] & 1);
  172.  
  173.     while( source<finish )
  174.     {
  175. /*
  176.     source[4]    S-FT ---P
  177.     source[5]    TTTT TTTT
  178.     source[6]   CCCC CCCX
  179.     source[7]    XXXX XXXX
  180.     source[8]    ---T -S-F
  181.     source[9]   YYYY YYYY
  182. */
  183.         {
  184.             unsigned char attrs = source[4];
  185.             unsigned char attr2 = source[8];
  186.             unsigned char color = source[6];
  187.             int sx = source[7] + (color & 0x01)*256; /* need adjust for left clip */
  188.             int sy = -source[9];
  189.             int flipx = attrs & 0x20;
  190.             int flipy = attr2 & 0x01;
  191.             int tall = (attr2 & 0x04) ? 1 : 0;
  192.             int wide = (attrs & 0x80) ? 1 : 0;
  193.             int pri = attrs & 0x01;
  194.             int sprite_number = (source[5] & 0xff)*4;
  195.             int row,col;
  196.  
  197.             if (pri == priority)
  198.             {
  199.                 if ((attrs & 0x10) && !wide) sprite_number += 1;
  200.                 if ((attr2 & 0x10) && !tall) sprite_number += 2;
  201.                 color = color >> 1;
  202.  
  203.                 if( sx > 512 - 32 ) sx -= 512;
  204.  
  205.                 if( flipx && !wide ) sx -= 16;
  206.                 if( !tall ) sy += 16;
  207.                 if( !tall && (attr2 & 0x10) && flipy ) sy -= 16;
  208.  
  209.                 sx += sprite_xoffs;
  210.                 sy -= sprite_yoffs;
  211.  
  212.                 for( row=0; row<=tall; row++ )
  213.                 {
  214.                     for( col=0; col<=wide; col++ )
  215.                     {
  216.                         if (flipscreen)
  217.                         {
  218.                             drawgfx( bitmap, Machine->gfx[5],
  219.                                 sprite_number+2*row+col,
  220.                                 color,
  221.                                 !flipx,!flipy,
  222.                                 512-67 - (sx+16*(flipx ? 1-col : col)),
  223.                                 64-16-209 - (sy+16*(flipy ? 1-row : row)),
  224.                                 clip,
  225.                                 TRANSPARENCY_PEN, 0xf );
  226.                         }
  227.                         else
  228.                         {
  229.                             drawgfx( bitmap, Machine->gfx[5],
  230.                                 sprite_number+2*row+col,
  231.                                 color,
  232.                                 flipx,flipy,
  233.                                 -87 + (sx+16*(flipx ? 1-col : col)),
  234.                                 209 + (sy+16*(flipy ? 1-row : row)),
  235.                                 clip,
  236.                                 TRANSPARENCY_PEN, 0x0f );
  237.                         }
  238.                     }
  239.                 }
  240.             }
  241.         }
  242.         source+=16;
  243.     }
  244. }
  245.  
  246. static void mark_textlayer_colors(void)
  247. {
  248.     int i, offs;
  249.     unsigned short palette_map[512];
  250.  
  251.     memset (palette_map, 0, sizeof (palette_map));
  252.  
  253.     for (offs = 0; offs < 0x400; offs++)
  254.     {
  255.         palette_map[(baraduke_textram[offs+0x400] << 2) & 0x1ff] |= 0xffff;
  256.     }
  257.  
  258.     /* now build the final table */
  259.     for (i = 0; i < 512; i++)
  260.     {
  261.         int usage = palette_map[i], j;
  262.         if (usage)
  263.         {
  264.             for (j = 0; j < 4; j++)
  265.                 if (usage & (1 << j))
  266.                     palette_used_colors[i * 4 + j] |= PALETTE_COLOR_VISIBLE;
  267.         }
  268.     }
  269. }
  270.  
  271. static void mark_sprites_colors(void)
  272. {
  273.     int i;
  274.     const unsigned char *source = &spriteram[0];
  275.     const unsigned char *finish = &spriteram[0x0800-16];/* the last is NOT a sprite */
  276.  
  277.     unsigned short palette_map[128];
  278.  
  279.     memset (palette_map, 0, sizeof (palette_map));
  280.  
  281.     while( source<finish )
  282.     {
  283.         palette_map[source[6] >> 1] |= 0xffff;
  284.         source+=16;
  285.     }
  286.  
  287.     /* now build the final table */
  288.     for (i = 0; i < 128; i++)
  289.     {
  290.         int usage = palette_map[i], j;
  291.         if (usage)
  292.         {
  293.             for (j = 0; j < 16; j++)
  294.                 if (usage & (1 << j))
  295.                     palette_used_colors[i * 16 + j] |= PALETTE_COLOR_VISIBLE;
  296.         }
  297.     }
  298. }
  299.  
  300. void baraduke_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  301. {
  302.     int offs;
  303.  
  304.     /* this is the global sprite Y offset, actually */
  305.     flipscreen = spriteram[0x07f6] & 0x01;
  306.     tilemap_set_flip(ALL_TILEMAPS,flipscreen ? (TILEMAP_FLIPY | TILEMAP_FLIPX) : 0);
  307.  
  308.     tilemap_update(ALL_TILEMAPS);
  309.  
  310.     palette_init_used_colors();
  311.     mark_textlayer_colors();
  312.     mark_sprites_colors();
  313.     if (palette_recalc())
  314.         tilemap_mark_all_pixels_dirty(ALL_TILEMAPS);
  315.  
  316.     tilemap_render(ALL_TILEMAPS);
  317.  
  318.     tilemap_draw(bitmap,tilemap[1],TILEMAP_IGNORE_TRANSPARENCY);
  319.     draw_sprites(bitmap,0);
  320.     tilemap_draw(bitmap,tilemap[0],0);
  321.     draw_sprites(bitmap,1);
  322.  
  323.     for (offs = 0x400 - 1; offs > 0; offs--)
  324.     {
  325.         int mx,my,sx,sy;
  326.  
  327.         mx = offs % 32;
  328.         my = offs / 32;
  329.  
  330.         if (my < 2)
  331.         {
  332.             if (mx < 2 || mx >= 30) continue; /* not visible */
  333.             sx = my + 34; sy = mx - 2;
  334.         }
  335.         else if (my >= 30)
  336.         {
  337.             if (mx < 2 || mx >= 30) continue; /* not visible */
  338.             sx = my - 30; sy = mx - 2;
  339.         }
  340.         else
  341.         {
  342.             sx = mx + 2; sy = my - 2;
  343.         }
  344.         if (flipscreen)
  345.         {
  346.                 sx = 35 - sx; sy = 27 - sy;
  347.         }
  348.  
  349.         drawgfx(bitmap,Machine->gfx[0],    baraduke_textram[offs],
  350.                 (baraduke_textram[offs+0x400] << 2) & 0x1ff,
  351.                 flipscreen,flipscreen,sx*8,sy*8,
  352.                 &Machine->drv->visible_area,TRANSPARENCY_PEN,3);
  353.     }
  354. }
  355.  
  356. void metrocrs_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  357. {
  358.     int offs;
  359.  
  360.     /* this is the global sprite Y offset, actually */
  361.     flipscreen = spriteram[0x07f6] & 0x01;
  362.     tilemap_set_flip(ALL_TILEMAPS,flipscreen ? (TILEMAP_FLIPY | TILEMAP_FLIPX) : 0);
  363.  
  364.     tilemap_update(ALL_TILEMAPS);
  365.  
  366.     if (palette_recalc())
  367.         tilemap_mark_all_pixels_dirty(ALL_TILEMAPS);
  368.  
  369.     tilemap_render(ALL_TILEMAPS);
  370.  
  371.     tilemap_draw(bitmap,tilemap[0],TILEMAP_IGNORE_TRANSPARENCY);
  372.     draw_sprites(bitmap,0);
  373.     tilemap_draw(bitmap,tilemap[1],0);
  374.     draw_sprites(bitmap,1);
  375.     for (offs = 0x400 - 1; offs > 0; offs--)
  376.     {
  377.         int mx,my,sx,sy;
  378.  
  379.         mx = offs % 32;
  380.         my = offs / 32;
  381.  
  382.         if (my < 2)
  383.         {
  384.             if (mx < 2 || mx >= 30) continue; /* not visible */
  385.             sx = my + 34; sy = mx - 2;
  386.         }
  387.         else if (my >= 30)
  388.         {
  389.             if (mx < 2 || mx >= 30) continue; /* not visible */
  390.             sx = my - 30; sy = mx - 2;
  391.         }
  392.         else
  393.         {
  394.             sx = mx + 2; sy = my - 2;
  395.         }
  396.         if (flipscreen)
  397.         {
  398.                 sx = 35 - sx; sy = 27 - sy;
  399.         }
  400.         drawgfx(bitmap,Machine->gfx[0],    baraduke_textram[offs],
  401.                 (baraduke_textram[offs+0x400] << 2) & 0x1ff,
  402.                 flipscreen,flipscreen,sx*8,sy*8,
  403.                 &Machine->drv->visible_area,TRANSPARENCY_PEN,3);
  404.     }
  405. }
  406.